package com.itmg.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.itmg.dao.TopImageDAO;
import com.itmg.dao.model.TopImageModel;

public class TopImageDAOImpl extends SqlMapClientDaoSupport implements TopImageDAO {

	public List<TopImageModel> getTopImage() throws Exception {
		List<TopImageModel> list = new ArrayList<TopImageModel>();
		String sql = "select * from shop_top order by create_time desc";
		Connection con = null;
		try{
			con = getSqlMapClientTemplate().getDataSource().getConnection();
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				TopImageModel model = new TopImageModel();
				model.setId(rs.getInt("id"));
				model.setName(rs.getString("name"));
				model.setContent(rs.getString("content"));
				model.setDetail_url(rs.getString("detail_url"));
				model.setImage_url(rs.getString("image_url"));
				model.setIsindex(rs.getInt("isindex"));
				list.add(model);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	public void save(TopImageModel model) throws Exception {
		 Connection con = null;
		 String sql ="insert into shop_top (" +
			 		"name," +
			 		"content," +
			 		"detail_url," +
			 		"image_url," +
			 		"isindex) values (" +
			 		"?," +
			 		"?," +
			 		"?," +
			 		"?," +
			 		"?)";
		 try{
			 con = getSqlMapClientTemplate().getDataSource().getConnection();
			 PreparedStatement ps = con.prepareStatement(sql);
			 ps.setString(1,model.getName());
			 ps.setString(2,model.getContent());
			 ps.setString(3,model.getDetail_url());
			 ps.setString(4,model.getImage_url());
			 ps.setInt(5,model.getIsindex());
			 ps.executeUpdate();
		 }catch(Exception e){
			 e.printStackTrace();
		 }finally{
			 try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		 }
	}

	@Override
	public void delete(int id) throws Exception {
		String sql = "delete from shop_top where id=?";
		Connection con = null;
		try{
			con = getSqlMapClientTemplate().getDataSource().getConnection();
			PreparedStatement ps = con.prepareStatement(sql);
			ps.setInt(1, id);
			ps.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}

	@Override
	public List<TopImageModel> getAllTopImage(int start, int lenght)
			throws Exception {
		List<TopImageModel> list = new ArrayList<TopImageModel>();
		String sql = "select * from shop_top limit ?,?";
		Connection con = null;
		try{
			con = getSqlMapClientTemplate().getDataSource().getConnection();
			PreparedStatement ps = con.prepareStatement(sql);
			ps.setInt(1, start);
			ps.setInt(2, lenght);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				TopImageModel model = new TopImageModel();
				model.setId(rs.getInt("id"));
				model.setName(rs.getString("name"));
				model.setContent(rs.getString("content"));
				model.setDetail_url(rs.getString("detail_url"));
				model.setImage_url(rs.getString("image_url"));
				model.setIsindex(rs.getInt("isindex"));
				list.add(model);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return list;
	}

	@Override
	public void update(TopImageModel model) throws Exception {
		Connection con = null;
		 String sql ="update shop_top set name=?,content=?,detail_url=?,image_url=?,isindex=?";
		 try{
			 con = getSqlMapClientTemplate().getDataSource().getConnection();
			 PreparedStatement ps = con.prepareStatement(sql);
			 ps.setString(1,model.getName());
			 ps.setString(2,model.getContent());
			 ps.setString(3,model.getDetail_url());
			 ps.setString(4,model.getImage_url());
			 ps.setInt(5,model.getIsindex());
			 ps.executeUpdate();
		 }catch(Exception e){
			 e.printStackTrace();
		 }finally{
			 try {
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		 }
	}

}
